#include <windows.h>
#include <math.h>
#include "CShopBuyMenuPacket.h"
#include "CChar.h"

CShopBuyMenuPacket::CShopBuyMenuPacket(unsigned char fameLevel) {
	numItems = 0;
	fameMult = 0.00;

	calcFameMultiplier(fameLevel);

	this->type = 0x3C;
	this->size = 0x04;

	memset(data, 0, sizeof(data));

}

CShopBuyMenuPacket::~CShopBuyMenuPacket() {
}

void CShopBuyMenuPacket::addItem(unsigned short itemID, unsigned long price) {

	this->size += 0x04;

	unsigned long shopPrice = 0;
	shopPrice = (int)ceil(price * fameMult);

	memcpy(data+0x08+(0x08*numItems)-0x04, &shopPrice, 4);
	memcpy(data+0x0C+(0x08*numItems)-0x04, &itemID, 2);
	memcpy(data+0x0E+(0x08*numItems)-0x04, &numItems, 1);

	numItems++;
}

unsigned char CShopBuyMenuPacket::getNumItems() {
	return numItems;
}

void CShopBuyMenuPacket::sendPacket(CChar * schar) {
	schar->setShop(data);
	schar->quePacket(*this);
}

void CShopBuyMenuPacket::calcFameMultiplier(unsigned char fameLevel) {
	// fame level 1 = 1.20
	// fame level 9 = 1.00
	fameMult = 1 + (0.20 * (9 - fameLevel) / 8);
}

